<?php

namespace App\Models\Post;

use App\Models\BaseModel;

class PostPraise extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'post_id',
        'user_id',
        'status',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'created_at',
        'updated_at'
    ];
    protected $table = 'post_praise';
    protected $dateFormat = 'U';

    /**
     * 关联用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(){
        return $this->belongsTo('App\Models\User\User', 'user_id', 'user_id');
    }

    /**
     * 获取赞
     * @param $page
     * @param $size
     * @param $post_id
     * @return array
     */
    public function getPraises($page, $size, $post_id){
        $data = [];
        $data['list'] = $this->select('post_praise.*')
            ->rightJoin('users',function($join) {
                $join->on('post_praise.user_id', '=', 'users.user_id')->where('users.status', 1);
            })
            ->withOnly('user', ['user_id', 'cover', 'nickname', 'vip_level'])
            ->where('post_praise.status', 1)
            ->where('post_praise.post_id', $post_id)
            ->forPage($page, $size+1)
            ->orderBy('post_praise.created_at', 'desc')
            ->get()
            ->toArray();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }

}
